window._MXQ = {

    _CURRENTLABEL:null,

    _EQUIP:null,

    _XWTYPE:null,

    _XWOBJ:null,

    _XWARR:null,

    _XWINDEX:0,

    showXQ:function(parentNode,param){
        var uiObj = _GRM.getNodeObj('xiangqian');
        var mainLabel = uiObj.node;

        _MXQ._CURRENTLABEL = mainLabel;

        _MXQ._EQUIP = null;
        _MXQ._XWTYPE = null;
        _MXQ._XWOBJ = null;
        _MXQ._XWARR = null;
        _MXQ._XWINDEX = null;

        if(uiObj.isnew){
            _MXQ.initButton(mainLabel);
        }

        _MXQ.switchTips(true);
        _MXQ.refreshEquip();
        _MXQ.refreshGoods();
        parentNode.addChild(mainLabel);
        return mainLabel;
    },

    initButton:function(mainLabel){
        mainLabel.getChildByName('mainLabel')
        .getChildByName('base')
        .getChildByName('clear').on(cc.Node.EventType.TOUCH_START, function(){
            _MXQ.realXq();
        }, mainLabel);
    },

    switchTips:function(showtips){

        var mainLabel = _MXQ._CURRENTLABEL;
        var goodsNode = mainLabel.getChildByName('mainLabel')
                            .getChildByName('base');
        var tipsNode = mainLabel.getChildByName('mainLabel')
                            .getChildByName('tips')

        if(showtips){
            tipsNode.active = true;
            goodsNode.active = false;
        }else{
            tipsNode.active = false;
            goodsNode.active = true;
        }

    },

    setEquip:function(equip){

        console.log(equip);

        _MXQ.switchTips(false);

        _MXQ._EQUIP = equip;

        _MXQ.refreshEquip();
        
    },

    setXq:function(tempGoodsInfo,goodsIndex,arr){

        console.log(tempGoodsInfo,goodsIndex,arr);

        _MXQ.switchTips(false);

        _MXQ._XWARR = arr;
        _MXQ._XWINDEX = goodsIndex;
        _MXQ._XWOBJ = tempGoodsInfo;
        _MXQ._XWTYPE = _FIGHTITEM[tempGoodsInfo.type].xw;

        _MXQ.refreshGoods();
    },

    refreshEquip:function(){
        var mainLabel = _MXQ._CURRENTLABEL;
        var equipInfo  = _MXQ._EQUIP;
        var equipNode = mainLabel.getChildByName('mainLabel')
                        .getChildByName('base')
                        .getChildByName('equip');
        var xwNodes = mainLabel.getChildByName('mainLabel')
                        .getChildByName('base')
                        .getChildByName('content').getChildren();
        for(var i=0;i<xwNodes.length;i++){
                            xwNodes[i].active = false;
                        }

        if(equipInfo){
            equipNode.active = true;
            var imgNode = equipNode.getChildByName('imgs')
                                .getChildByName('img');
            var nameNode = equipNode.getChildByName('imgs')
                                .getChildByName('name');
            

            imgNode.getComponent(cc.Sprite).spriteFrame = _GRM.getEquipFrame(_IOF.getGoodsImg(equipInfo));
            nameNode.getComponent(cc.Label).string = equipInfo.type;
            equipNode.color = new cc.color(_IOF.getGoodsColor(equipInfo));

            
            var xwindex = 0;
            for(var key in equipInfo.xw){
                _MXQ.refreshXwNode(xwNodes[xwindex],key);
                xwindex++;
            }
        }else{
            equipNode.active = false;
        }
    },

    refreshXwNode:function(node,xw){

        if(node){
            node.active = true;
            var msg = _XW[xw].label+"\n";
            var fsxarr = _XW[xw].fsx;
            for(var i=0;i<fsxarr.length;i++){
                msg+= _DICT[fsxarr[0].type]+fsxarr[0].base+" ";
            }
            node.getChildByName('name').getComponent(cc.Label).string = msg;
        }

    },

    refreshGoods:function(){

        var mainLabel = _MXQ._CURRENTLABEL;
        var goodsNode = mainLabel.getChildByName('mainLabel')
                            .getChildByName('base')
                            .getChildByName('goods');
        var xwInfo = _MXQ._XWOBJ;
        if(xwInfo!=null){
            goodsNode.active = true;
            var imgNode = goodsNode.getChildByName('imgs')
                            .getChildByName('img');
            var nameNode = goodsNode.getChildByName('imgs')
                            .getChildByName('name');
            imgNode.getComponent(cc.Sprite).spriteFrame = _GRM.getEquipFrame(_IOF.getGoodsImg(xwInfo));
            goodsNode.color = new cc.color(_IOF.getGoodsColor(xwInfo));
            nameNode.getComponent(cc.Label).string = xwInfo.type;
        }else{
            goodsNode.active = false;
        }

    },

    realXq:function(){
        console.log('镶嵌...');

        if(!_MXQ._EQUIP){
            _TIPS.showMessage('请先选择装备');
            return;
        }

        if(!_MXQ._XWOBJ){
            _TIPS.showMessage('请先选择符文');
            return;
        }

        

        if(!_MXQ._EQUIP.xw){
            _MXQ._EQUIP.xw={};
        }

        var xwnmb = 0;
        for(var key in _MXQ._EQUIP.xw){
            xwnmb++;
            if(key==_MXQ._XWTYPE){
                _TIPS.showMessage('装备修为重复');
                return;
            }
        }
        if(xwnmb>=5){
            _TIPS.showMessage('装备修为数不能超过5条');
            return;
        }

        _CU.cutArrNmb(_MXQ._XWARR,_MXQ._XWOBJ.type)
        _MXQ._EQUIP.xw[_MXQ._XWTYPE]=0;
        _MXQ._EQUIP.qua = _MXQ.getNextQua(_MXQ._EQUIP.qua);
        _MXQ._XWOBJ = null;
        _MXQ._XW = null;
        _MXQ._XWARR = null;
        _MXQ._XWINDEX=0;
        _MXQ.refreshEquip();
        _MXQ.refreshGoods();
        _MG.refreshPage(_MG._CURRENTLABEL);
    },

    getNextQua:function(qua){
        if(qua=="D"){
            return "C";
        }else if(qua=="C"){
            return "B";
        }else if(qua=="B"){
            return "A";
        }else if(qua=="A"){
            return "S";
        }else if(qua=="S"){
            return "SS";
        }
        return "SS";
    }

}